﻿using UnityEngine;
using System.Collections;

public class GoBackButton : MonoBehaviour {

	void OnClick()
	{
		LoadingWindow.window.ShowShortLoading();
		glog.debug("m_Scene ---" + CSceneManager.m_Scene);

		if (CSceneManager.m_Scene == SCENE.UnitInvenScene)
		{
			CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
            if (CSceneManager.prevSceneIndex == SCENE.UnitMenuScene)
				c.GoToUnitMenuScene();
			else if( CSceneManager.prevSceneIndex == SCENE.UnitLevelupScene )
				c.GoToUnitLevelUpScene();
			else if(CSceneManager.prevSceneIndex == SCENE.UnitEvolutionScene )
				c.GoToUnitPowerUpScene();
			else
				c.GoToDeckEditScene();
		}

		else if (CSceneManager.m_Scene == SCENE.DeckEditScene)
		{
            if (CSceneManager.prevSceneIndex == SCENE.GameReadyScene)
			{
				CSceneManager.manager.GoToGameReadyScene();
			}
			else
			{
				CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
				c.GoToWaitroomScene();
			}
		}

		else if (CSceneManager.m_Scene == SCENE.UnitChangeConfirmScene)
		{
			CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
			c.GoToUnitInvenScene();
		}
		
		else if (CSceneManager.m_Scene == SCENE.SummonMenuScene || CSceneManager.m_Scene == SCENE.SummonResultScene || CSceneManager.m_Scene == SCENE.UnitMenuScene )
		{
			RestAPI.singleton.GetMyUnitList(ReturnCallback);
//			CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
//			c.GoToWaitroomScene();
		}
		else if(CSceneManager.m_Scene == SCENE.SelectUnitScene)
		{
			CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
			if( CSceneManager.prevSceneIndex == SCENE.UnitLevelupScene )
				c.GoToUnitLevelUpScene();
			else if(CSceneManager.prevSceneIndex == SCENE.UnitEvolutionScene )
				c.GoToUnitPowerUpScene();
			else
				c.GoToDeckEditScene();
		}
        else if (CSceneManager.m_Scene == SCENE.UnitLevelupScene)
        {
//            RestAPI.singleton.GetMyUnitList(ReturnCallback);
			DeckCardSelectSceneManager.SelectCardInfos.Clear();
			UnitComposeManager.originalUnitInfo = null;
            if(UnitComposeManager.materialUnitInfo != null)
			    UnitComposeManager.materialUnitInfo.Clear();
			UnitInfo.AllUnitInfosUnCheck(MyInfo.Instance.MyUnitsList);
			CSceneManager.manager.GoToWaitroomScene();
        }
        else if (CSceneManager.m_Scene == SCENE.ShopScene)
        {
            RestAPI.singleton.RefreshUserData(ReturnCallback);
            RestAPI.singleton.GetPVEStageInfo(ReturnCallback);
        }
        else
        {
            CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
            c.GoToWaitroomScene();
        }
	}

    void ReturnCallback(Hashtable returnData)
    {
		Debug.LogError(CSceneManager.prevSceneIndex);
        switch ((RestAPIProtocol)returnData["protocol"])
        {
            case RestAPIProtocol.REFRESHUSERINFO:
            case RestAPIProtocol.GETMYUNITLIST:
                if (CSceneManager.prevSceneIndex == SCENE.GameReadyScene)
				{
					LoadingWindow.window.HideLoading();
					CSceneManager.manager.GoToGameReadyScene();
				}
				else if( CSceneManager.prevSceneIndex == SCENE.UnitLevelupScene )
				{
					LoadingWindow.window.HideLoading();
					CSceneManager.manager.GoToUnitMenuScene();
				}
				else
				{
	                CSceneManager.manager.GoToWaitroomScene();
				}
                break;
            case RestAPIProtocol.GETPVESTAGEINFO:
                break;
        }
    }
}
